<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <div class="header">
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="100px"
            class="flex-start"
          >
            <el-form-item
              v-if="userType == '00'"
              label="所属网点"
              prop="networkLevel"
            >
              <el-select
                v-model="form.networkLevel"
                placeholder="请选择所属网点"
                style="width: 275px"
                @change="bg"
              >
                <el-option
                  v-for="item in networkList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="时间" prop="time">
              <el-date-picker
                v-model="form.time"
                style="width: 650px"
                type="daterange"
                range-separator="至"
                value-format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="mxDateChange"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="mxClear">重置</el-button>
              <el-button icon="el-icon-upload2" @click="exportData"
                >导出</el-button
              >
              <!-- <el-button plain icon="el-icon-setting" @click="openSetting"
                >质控费用配置参数</el-button
              > -->
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <div v-if="!isObjectEmpty(tableData)">
          <el-table
            ref="multipleTable"
            v-loading="loading"
            :data="tableData.list.data"
            tooltip-effect="dark"
            style="width: 100%"
            border
            row-key="id"
            :tree-props="{ children: 'children' }"
            max-height="640"
          >
            <el-table-column
              v-for="(t, n) in tableData.fields"
              :key="n"
              :prop="t.fields"
              :label="t.translate"
              :fixed="n < 4"
              :min-width="
                flexColumnWidth(t.translate, t.fields, tableData.list.data)
              "
              align="center"
            >
              <template slot-scope="scope">
                <a
                  v-if="t.isPoint != null"
                  style="color: skyblue"
                  @click="searchDetail(scope.row, t)"
                  >{{ scope.row[t.fields] }}</a
                >
                <span v-else>{{ scope.row[t.fields] }}</span>
              </template>
              <el-table-column
                v-for="(s, i) in t.sunList"
                v-show="s.sunList != null"
                :key="i"
                :prop="s.fields"
                :label="s.translate"
                :min-width="
                  flexColumnWidth(s.translate, s.fields, tableData.list.data)
                "
                align="center"
              >
                <template slot-scope="scope">
                  <a
                    v-if="s.isPoint != null"
                    style="color: skyblue"
                    @click="searchDetail(scope.row, s)"
                    >{{ scope.row[s.fields] }}</a
                  >
                  <span v-else>{{ scope.row[s.fields] }}</span>
                </template>
                <el-table-column
                  v-for="(a, b) in s.sunList"
                  v-show="a.sunList != null"
                  :key="b"
                  :prop="a.fields"
                  :label="a.translate"
                  :min-width="
                    flexColumnWidth(a.translate, a.fields, tableData.list.data)
                  "
                  align="center"
                >
                  <template slot-scope="scope">
                    <a
                      v-if="a.isPoint != null"
                      style="color: skyblue"
                      @click="searchDetail(scope.row, a)"
                      >{{ scope.row[a.fields] }}</a
                    >
                    <span v-else>{{ scope.row[a.fields] }}</span>
                  </template>
                </el-table-column>
              </el-table-column>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="isObjectEmpty(tableData)">
          <empty />
        </div>
      </el-col>
    </el-row>
    <div class="block" style="display: flex; justify-content: flex-end">
      <el-pagination
        :current-page="form.pageDate.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="form.pageDate.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      title="质控费用配置参数"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form :model="updateParams">
        <el-form-item label="所属网点" :label-width="formLabelWidth" required>
          <el-row>
            <el-col>
              <div style="width: 50%">
                <networkSelect
                  :value="'请选择所属网点'"
                  @networkCode="handleSelect"
                />
              </div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="选择理赔类型" :label-width="formLabelWidth">
          <el-row>
            <el-col>
              <el-select
                v-model="updateParams.type"
                placeholder="选择理赔类型"
                style="width: 50%"
                @change="changeType"
              >
                <el-option :value="1" label="出港理赔公司给承包区" />
                <el-option :value="2" label="进港三件理赔承包区赔公司" />
              </el-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="遗失" :label-width="formLabelWidth">
          <el-input
            v-model="params.lost"
            :disabled="updateParams == null"
            style="width: 50%"
            maxlength="7"
          />
        </el-form-item>
        <el-form-item label="延误" :label-width="formLabelWidth">
          <el-input
            v-model="params.delayed"
            :disabled="updateParams == null"
            style="width: 50%"
            maxlength="7"
          />
        </el-form-item>
        <el-form-item label="破损" :label-width="formLabelWidth">
          <el-input
            v-model="params.damaged"
            :disabled="updateParams == null"
            style="width: 50%"
            maxlength="7"
          />
        </el-form-item>
        <el-form-item label="超派件" :label-width="formLabelWidth">
          <el-input
            v-model="params.overdelivery"
            :disabled="updateParams == null"
            style="width: 50%"
            maxlength="7"
          />
        </el-form-item>
        <el-form-item label="三件申诉返款" :label-width="formLabelWidth">
          <el-input
            v-model="params.threeAppealRefund"
            :disabled="updateParams == null"
            style="width: 50%"
            maxlength="7"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="uodateParams">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getOutletsList,
  getSubjectFirstList,
  getSubjectList,
  // subjectSearch,
  // addSubject
} from "@/api/dashboard";
import {
  QualityControlCostsinit,
  qualityControlFeeSearch,
  qualityControlFeeUpdate,
} from "@/api/apifile/Networkfinance";
import empty from "@/components/menuComponent/empty.vue";
import networkSelect from "@/components/menuComponent/NetworkSelect.vue";
import { exportDataZKFY } from "@/api/home";

export default {
  components: { empty, networkSelect },

  data() {
    return {
      userType: localStorage.getItem("userType") || null,
      form: {
        networkLevel: 0,
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      formLabelWidth: "120px",
      updateParams: {
        code: null,
        type: 1,
      },
      params: {
        id: null,
        lost: null,
        delayed: null,
        damaged: null,
        overdelivery: null,
        threeAppealRefund: null,
      },
      addForm: {
        networkNameId: "",
        firstId: "",
        secondName: "",
      },
      // 一级网点
      SubjectFirstList: [],
      // 网点列表
      networkList: [
        { value: 0, label: "网点" },
        { value: 3, label: "落实承包区" },
        { value: 4, label: "落实客户" },
      ],
      pages: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      loading: false,
      dialogFormVisible: false,
      currentPage4: 4,
      tableData: {
        list: {
          data: [],
        },
      },
      rules: {
        networkLevel: [
          { required: true, message: "请选择网点名称", trigger: "blur" },
        ],
        time: [{ required: true, message: "请选择日期", trigger: "blur" }],
      },
      searchForm: {
        contractArea: "",
        settleNetworkNameId: "",
        type: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
    };
  },
  // created() {
  //   this.getList();
  //   this.getOutLet();
  //   this.getSubjectFirst();
  // },
  methods: {
    searchDetail(item, type) {
      this.searchForm.contractArea = this.networkList.filter(
        (el) => el.value === this.form.networkLevel
      )[0].label;
      this.searchForm.settleNetworkNameId = item.name;
      this.searchForm.type = type.isPoint;
      this.$store.state.app.zkfytab = this.searchForm;
      this.$router.push({
        path: "QualityControlCostsDetail",
      });
    },
    exportData() {
      this.$refs.form.validate((res) => {
        if (res) {
          this.$notify.info({
            title: "开始准备下载...",
          });
          exportDataZKFY(this.form).then((res) => {
            const url = window.URL.createObjectURL(new Blob([res]), {
              type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            });
            const link = document.createElement("a");
            link.href = url;
            link.setAttribute("download", "质控费用汇总报表.xlsx"); // 设置下载的文件名
            document.body.appendChild(link);
            link.click();
            link.parentNode.removeChild(link);
            this.$notify({
              title: "下载成功",
              type: "success",
            });
          });
        }
      });
    },
    isObjectEmpty(obj) {
      return Object.keys(obj).length === 0;
    },
    bg(value) {
      // // console.log(value)
      if (value === 2) {
        this.isart = true;
      } else {
        this.isart = false;
      }
    },
    handleSizeChange(val) {
      this.form.pageDate.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.form.pageDate.pageNum = val;
      this.init();
    },
    mxClear() {
      this.$refs.form.resetFields();
      this.form.startTime = "";
      this.form.endTime = "";
    },
    search() {
      this.$refs.form.validate((res) => {
        if (res) {
          this.init();
        }
      });
    },
    init() {
      this.loading = true;
      QualityControlCostsinit(this.form).then((res) => {
        // // console.log('查询', res)
        this.loading = false;
        if (res.code === 200) {
          this.tableData = res.data;
          this.total = res.data.list.total;
          this.form.pageDate.pageNum = res.data.list.current;
        } else {
          this.tableData = {
            fields: [],
            list: {
              data: [],
            },
          };
          this.total = 0;
        }
      });
    },
    mxDateChange(value) {
      if (value != null) {
        this.searchForm.startTime = value[0];
        this.searchForm.endTime = value[1];
        this.form.startTime = value[0];
        this.form.endTime = value[1];
      }
    },
    // ------------------------------------
    // 获取网点列表
    getOutLet() {
      getOutletsList().then((res) => {
        if (res.code === 200) {
          this.networkList = res.data;
        }
      });
    },
    // 获取一级网点下拉框
    getSubjectFirst() {
      getSubjectFirstList().then((res) => {
        if (res.code === 200) {
          this.SubjectFirstList = res.data;
        }
      });
    },
    // 显示表格数据
    getList() {
      getSubjectList().then((res) => {
        if (res.code === 200) {
          this.tableData = res.data.data;
          this.total = res.data.total;
          this.pages = res.data.pages;
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    // 打开配置弹窗
    openSetting() {
      this.dialogFormVisible = true;
    },
    handleSelect(val) {
      this.updateParams.code = val;
      if (val != null) {
        this.getSelectContractAreaSend();
      }

      // // console.log('val', val)
    },
    // 改变参数
    changeType(val) {
      if (this.updateParams.code === null) {
        this.$message("请选择所属网点");
        return;
      }
      this.updateParams.type = val;
      this.getSelectContractAreaSend();
    },
    // 查询质控费用汇总配置
    getSelectContractAreaSend() {
      // // console.log(this.updateParams)
      qualityControlFeeSearch(this.updateParams).then((res) => {
        // console.log("res", res);
        if (res.code === 200) {
          this.params.id = res.data.id;
          this.params.lost = res.data.lost;
          this.params.delayed = res.data.delayed;
          this.params.damaged = res.data.damaged;
          this.params.overdelivery = res.data.overdelivery;
          this.params.threeAppealRefund = res.data.threeAppealRefund;
        } else {
          this.params = {
            id: null,
            lost: null,
            delayed: null,
            damaged: null,
            overdelivery: null,
            threeAppealRefund: null,
          };
        }
      });
    },
    // 修改质控费用配置列表
    uodateParams() {
      if (this.updateParams.code === null) {
        this.$message("请选择所属网点");
        return;
      }
      qualityControlFeeUpdate(this.params).then((res) => {
        if (res.code === 200) {
          this.dialogFormVisible = false;
          this.$message({ type: "success", message: "修改成功" });
          // this.search()
        } else {
          this.$message({ type: "error", message: "修改失败" });
        }
      });
    },

    // 重置
    clear() {
      this.form = {};
      this.getList();
    },

    handleSelectionChange(val) {
      // this.multipleSelection = val;
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  padding: 20px;
}

.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}

.flex-start {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}

.flex-bettom {
  display: flex;
  justify-content: space-between;
}

.header {
  padding-bottom: 20px;
  border-bottom: 1px solid #ccc;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
